// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.dcel.parser;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

import com.dustedpixels.daflan.Instance;
import com.dustedpixels.daflan.Unit;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
final class SystemInstanceCompiler {

  final Unit unit;

  public SystemInstance(Unit unit) {
    this.unit = unit;
  }
  
  Instance compile(Unit unit) {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    writer.visit(
        Opcodes.V1_5, 
        Opcodes.ACC_FINAL | Opcodes.ACC_PUBLIC,
        getClassName(unit),
        null,
        "java/lang/Object",
        new String[] {getClassName(Instance.class)});
    writer.visitField(
        Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, 
        "pinData",
        "java/util/Map",
        null,
        HashMapdesc, signature, value)
  }
  
  String getClassName(Unit unit) {
    String packageName = unit.getPackageName();
    if (packageName == null) {
      return getClassName(unit.getName());
    } else {
      return getClassName(packageName + "." + unit.getName());
    }
  }

  String getClassName(Class clazz) {
    return getClassName(clazz.getName());
  }

  String getClassName(String className) {
    return className.replace('.', '/');
  }
}
